package imvu.rbox.object
{
	import anshe.object.ChooseObject;
	import anshe.object.SliderObject;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import imvu.rbox.event.ActionEvent;
	import imvu.rbox.event.BoxEvent;
	import imvu.rbox.global.GStatic;
	import imvu.rbox.struct.CGameRoomInfo;

	public class CreateNewRoom extends Sprite
	{
		public var mcMask:Sprite;					//	遮照层
		public var sprBaseNumber:ChooseObject;		//	7 个方块
		public var sprMoreNumber:ChooseObject;		//	15 个方块
		// public var sprGameModel:ChooseObject;		//	游戏模式
		// public var sprTimeModel:ChooseObject;		//	计时模式
		
		// public var txtTime:TextField;			//	时间选择文字框
		
		public var bgSprite:Sprite;				//	背景框对象
		
		public var sprSpeed:SliderObject;		//	速度选择条
		// public var sprTime:SliderObject;		//	时间选择条
		
		public var btnOK:SimpleButton;			//	OK 按钮
		public var btnCancel:SimpleButton;		//	Cancel 按钮
		public var btnClose:SimpleButton;		//	Close 按钮
		
		public var mcHot:Sprite;		//	热响应遮照片
				 	
		public function CreateNewRoom()
		{
			this.cacheAsBitmap = true;
			
			InitObject();
			InitListener();
		}		
		
		public function HiddenThis():void
		{
			this.mcHot.graphics.clear();
			
			//	单选按钮事件
			this.sprBaseNumber.removeEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			this.sprMoreNumber.removeEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			// this.sprGameModel.removeEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			// this.sprTimeModel.removeEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			
			//	一般按钮事件
			this.btnOK.removeEventListener(MouseEvent.CLICK, onClickButton);
			this.btnCancel.removeEventListener(MouseEvent.CLICK, onClickButton);
			this.btnClose.removeEventListener(MouseEvent.CLICK, onClickButton);
			
			this.parent.removeChild(this);
		}
		
		//	设置面板是否可用标识
		public function set enabled(flag:Boolean):void
		{
			this.mcMask.visible = !flag;
		}
		
		private function InitObject():void
		{
			this.enabled = true;
			
			//	设置几个单选按钮状态及文字内容
			this.sprBaseNumber.text = BoxEvent.NORMAL_GAME + " box";
			this.sprBaseNumber.isClick = true;
			
			this.sprMoreNumber.text = BoxEvent.HARD_GAME + " box";
			this.sprMoreNumber.isClick = false;
			
			// this.sprGameModel.text = "Game model";
			// this.sprGameModel.isClick = true;
			
			// this.sprTimeModel.text = "Time model";
			// this.sprTimeModel.isClick = false;
			
			//	设置两个滑块的状态及初始值
			this.sprSpeed.min = BoxEvent.MIN_SPEED;
			this.sprSpeed.max = BoxEvent.MAX_SPEED;
			this.sprSpeed.percent = BoxEvent.MIN_SPEED;
			
			// this.sprTime.min = BoxEvent.MIN_TIME;
			// this.sprTime.max = BoxEvent.MAX_TIME;
			// this.sprTime.percent = BoxEvent.MIN_TIME;
			
			//	设置计时器对象的显示方式
			// this.txtTime.visible = false;
			// this.sprTime.visible = false;	
			
			mcHot.graphics.clear();
			mcHot.graphics.beginFill(0xFFFFFF, 0);
			mcHot.graphics.drawRect(-3000, -3000, 6000, 6000);
			mcHot.graphics.endFill();			
		}
		
		private function InitListener():void
		{
			//	单选按钮事件
			this.sprBaseNumber.addEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			this.sprMoreNumber.addEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			// this.sprGameModel.addEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			// this.sprTimeModel.addEventListener(ChooseObject.CLICK_THIS, onClickChoose);
			
			//	一般按钮事件
			this.btnOK.addEventListener(MouseEvent.CLICK, onClickButton);
			this.btnCancel.addEventListener(MouseEvent.CLICK, onClickButton);
			this.btnClose.addEventListener(MouseEvent.CLICK, onClickButton);
			
			//	特殊事件
			this.mcMask.addEventListener(MouseEvent.CLICK, function(e:*):void {});
		}
		
		private function onClickChoose(e:Event):void
		{
			switch(e.target)
			{
				case this.sprBaseNumber :
					this.sprBaseNumber.isClick = true;
					this.sprMoreNumber.isClick = false;
					break;
				
				case this.sprMoreNumber :
					this.sprBaseNumber.isClick = false;
					this.sprMoreNumber.isClick = true;
					break;
				/**
				case this.sprGameModel :
					this.sprGameModel.isClick = true;
					this.sprTimeModel.isClick = false;
					break;
				
				case this.sprTimeModel :
					this.sprGameModel.isClick = false;
					this.sprTimeModel.isClick = true;
					break; //**/
			}
			
			// this.txtTime.visible = this.sprTimeModel.isClick;
			// this.sprTime.visible = this.sprTimeModel.isClick;
		}
		
		private function onClickButton(e:MouseEvent):void
		{
			switch(e.target)
			{
				case this.btnOK :
					DoCreateRoom();
					break;
				
				default :
					HiddenThis();
					break;
			}
		}
		
		//	创建一个房间
		private function DoCreateRoom():void
		{
			var rInfo:CGameRoomInfo = new CGameRoomInfo();
			rInfo.master = GStatic.myInfo.userName;
			rInfo.isPlaying = false;
			rInfo.gameSpeed = this.sprSpeed.percent;
			// rInfo.boxNumber = (this.sprBaseNumber.isClick) ? BoxEvent.NORMAL_GAME : BoxEvent.HARD_GAME;
			// rInfo.gameModel = (this.sprGameModel.isClick) ? BoxEvent.GAME_MODEL : BoxEvent.TIME_MODEL;
			rInfo.boxNumber = (this.sprBaseNumber.isClick) ? 0 : 1;
			rInfo.gameModel = 1;//(this.sprGameModel.isClick) ? 1 : 0;
			rInfo.maxTime = 0;
			//if(this.sprTimeModel.isClick)
			//{
			//	rInfo.maxTime = this.sprTime.percent;				
			//}
			
			this.enabled = false;					
			this.dispatchEvent(new ActionEvent(ActionEvent.CREATE_NEW_ROOM, rInfo));
		}			
	}
}